package com.heeere.android.dnssdtuto;

import java.io.IOException;
import java.net.InetAddress;

import javax.jmdns.JmDNS;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceInfo;
import javax.jmdns.ServiceListener;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class DnssdDiscovery extends Activity {

	android.net.wifi.WifiManager.MulticastLock lock;
	android.os.Handler handler = new android.os.Handler();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		handler.postDelayed(new Runnable() {
			public void run() {
				setUp();
			}
		}, 1000);

	}

	/** Called when the activity is first created. */

	private String type = "_workstation._tcp.local.";
	private JmDNS jmdns = null;
	private ServiceListener listener = null;
	private ServiceInfo serviceInfo;

	private void setUp() {
		android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) getSystemService(android.content.Context.WIFI_SERVICE);
		lock = wifi.createMulticastLock("mylockthereturn");
		lock.setReferenceCounted(true);
		lock.acquire();
		try {
			String ip = Utils.getIPAddress(true);
			InetAddress _bindingAddress = InetAddress.getByName(ip);
			jmdns = JmDNS.create(_bindingAddress);
			jmdns.addServiceListener(type, listener = new ServiceListener() {

				@Override
				public void serviceResolved(ServiceEvent ev) {
					notifyUser("Service resolved: "
							+ ev.getInfo().getInet4Addresses()[0]+ " port:"
							+ ev.getInfo().getPort());
				
				}

				@Override
				public void serviceRemoved(ServiceEvent ev) {
					notifyUser("Service removed: " + ev.getName());
				}

				@Override
				public void serviceAdded(ServiceEvent event) {
					// Required to force serviceResolved to be called again
					// (after the first search)
					jmdns.requestServiceInfo(event.getType(), event.getName(),
							1);
					
				}
			});
			serviceInfo = ServiceInfo.create("_test._tcp.local.",
					"AndroidTest", 8893, "plain test service from android");
			jmdns.registerService(serviceInfo);
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
	}

	private void notifyUser(final String msg) {
		handler.postDelayed(new Runnable() {
			public void run() {
				TextView t = (TextView) findViewById(R.id.text);
				t.setText(msg + "\n=== " + t.getText());
			}
		}, 1);

	}

	@Override
	protected void onStart() {
		super.onStart();
		// new Thread(){public void run() {setUp();}}.start();
	}

	@Override
	protected void onStop() {
		if (jmdns != null) {
			if (listener != null) {
				jmdns.removeServiceListener(type, listener);
				listener = null;
			}
			jmdns.unregisterAllServices();
			try {
				jmdns.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			jmdns = null;
		}
		// repo.stop();
		// s.stop();
		lock.release();
		super.onStop();
	}
}